% Display app res, phase & asso errors
% with impedance tensor & erros as input
% tested 9/11/02 -ok
% changed the error bar plotting 12/11/02
% known bug - if (Rhxy - RhxyErr/2) <0, log plot will propmt an error message
% latest date 13.10.2002

function[Rhxy,Rhyx,Phxy,Phyx,RhxyErr,RhyxErr,PhxyErr,PhyxErr] = ts2resph(tf,ProcDef),

frq=ProcDef.TLFreq1;
nfrq = length(frq);

for i = 1:nfrq,   
   Phxy(i) = atan2(imag(tf(i,1)),real(tf(i,1))) * 180 / 3.1415926535897;
   Rhxy(i) = 0.2 * (1/frq(i)) * (tf(i,1) * conj(tf(i,1)));
   Phyx(i) = (atan2(imag(tf(i,2)),real(tf(i,2))) * 180 / 3.1415926535897);
   Rhyx(i) = 0.2 * (1/frq(i)) * (tf(i,2) * conj(tf(i,2)));
end;

k=find(Phyx>-180&Phyx<-90);
Phyx(k)=Phyx(k)+180;
k=find(Phxy<-180&Phxy<-90);
Phxy(k)=Phxy(k)+180;

k=find(Phyx>-90&Phyx<-0);
Phyx(k)=Phyx(k)+90;
k=find(Phxy>-90&Phxy<-0);
Phxy(k)=Phxy(k)+90;


PhxyErr = (tf(:,5)./abs(tf(:,1)))*(180/pi); 
PhyxErr = (tf(:,6)./abs(tf(:,2)))*(180/pi);
RhxyErr = (tf(:,5).*abs(tf(:,1))./frq') * 0.4; % bit confusion. may be ok -dt 2.8.2
RhyxErr = (tf(:,6).*abs(tf(:,2))./frq') * 0.4;

%--------------------------------------------------------------
